package com.TeamJigsaw.DanteTrail.Views;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.TeamJigsaw.DanteTrail.Debug.Debug;
import com.TeamJigsaw.DanteTrail.Models.Sprite;

public class GameScreenDisplay extends JPanel{
	private int playerStatus;
	
	private int partyMember1Status;
	
	private int partyMember2Status;
	
	private int partyMember3Status;
	
	private int level;
	
	private int screenSpeed;
	
	private Sprite player;
	
	private Sprite partyMember1;
	
	private Sprite partyMember2;
	
	private Sprite partyMember3;
	
	private Image bgImageFront;
	
	private Image bgImageBack;
	
	private Image wagon;
	
	private List<Sprite> bgImageMove;
	
	private Random generator;
	
	//static constants
	private final int DEAD = 5;
	
	static final int MOUNTAIN_HEIGHT = 40;
	
	static final int SCREEN_WIDTH = 500;
	
	static final int SCREEN_HEIGHT = 300;
	
	static final int WAGON_X = 300;
	
	static final int WAGON_Y = 225;
	
	public GameScreenDisplay() 
	{
		playerStatus = 0;
		partyMember1Status = 0;
		partyMember2Status = 0;
		partyMember3Status = 0;
		player = null;
		partyMember1 = null;
		partyMember2 = null;
		partyMember3 = null;
		level = -1;
		
		bgImageFront = null;
		bgImageBack = null;
		wagon = getImage("resources\\wagon.gif");
		bgImageMove = new ArrayList<Sprite>();		
		generator = new Random();
		createMountains(5);
	}

	/**
	 * Updates the sprite properties on the screen
	 * @param playerStatus The status of the player
	 * @param partyMember1Status The status of the 1st party member
	 * @param partyMember2Status The status of the 2nd party member
	 * @param partyMember3Status The status of the 3rd party member 
	 * @param playerImage The image of the player
	 * @param partyMember1Image The image of the 1st party member
	 * @param partyMember2Image The image of the 2nd party member
	 * @param partyMember3Image The image of the 3rd party member
	 * @param partyPace The pace of the party
	 * @param level The level of hell the party has reached
	 */
	public void updateScreen(int playerStatus, int partyMember1Status, 
			int partyMember2Status, int partyMember3Status, Image playerImage,
			Image partyMember1Image, Image partyMember2Image, Image partyMember3Image,
			int partyPace, int level) {
		this.playerStatus = playerStatus;
		this.partyMember1Status = partyMember1Status;
		this.partyMember2Status = partyMember2Status;
		this.partyMember3Status = partyMember3Status;
		this.player = new Sprite(playerImage,400,200);
		this.partyMember1 =  new Sprite(partyMember1Image,408,200);
		this.partyMember2 =  new Sprite(partyMember2Image,416,200);
		this.partyMember3 =  new Sprite(partyMember3Image,424,200);
		this.screenSpeed = partyPace * 2;
		
		if (this.level != level)
		{
			bgImageFront = new ImageIcon("resources\\" + level + "0bgm.gif").getImage();
			bgImageBack = new ImageIcon("resources\\" + level + "1bgm.gif").getImage();
			if (generator.nextInt(100) < 50) {
				Sprite newMountain = (new Sprite(
						new ImageIcon("resources\\" + level + "0" + 
								(generator.nextInt(3) + 1) + "bgm.gif").getImage(), 
						-100, MOUNTAIN_HEIGHT));
				bgImageMove.add(newMountain);
			}
		}
		
		update();
		
		repaint();
	}

	/**
	 * Updates all the sprites and moves them properly. 
	 */
	public void update()
	{
		for (Sprite mountain : bgImageMove) {
			mountain.setXSpeed(screenSpeed);
			mountain.move();
		}
	}
	
	/**
	 * Paints onto a graphics object
	 * @param g The graphics object to paint to
	 */
	@Override
	public void paint(Graphics g){
		super.paint(g);
	
		//clear the screen
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
		g.drawImage(bgImageBack,0,0,null);
		g.drawImage(bgImageFront,0,0,null);
		try {
			for (Sprite mountain : bgImageMove) {
				g.drawImage(mountain.getImage(), mountain.getX(), mountain.getY(), null);
			}
		}
		catch (Exception e) {
			Debug.println("Concurrent Modification Notification: System OK", Debug.INFO);
		}
		//draw all the partymembers to the screen
		// Image, x, y, width, height, Image observer
		//if (playerStatus == DEAD)
			g.drawImage(player.getImage(), player.getX(), player.getY(), null);
		//if (partyMember1Status == DEAD)
			g.drawImage(partyMember1.getImage(), partyMember1.getX(), partyMember1.getY(), null);
		//if (partyMember2Status == DEAD)
			g.drawImage(partyMember2.getImage(), partyMember2.getX(), partyMember2.getY(), null);
		//if (partyMember3Status == DEAD)
			g.drawImage(partyMember3.getImage(), partyMember3.getX(), partyMember3.getY(), null);
		
		g.drawImage(wagon, WAGON_X, WAGON_Y, null);
			
		repaint();
	}
	
	/**
	 * Creates mountains in the background
	 * @param no The number of mountains to be created
	 */
	private void createMountains(int no)
	{
		for (int i = 0; i < no; i++)
		{
			Sprite newMountain = (new Sprite(
					new ImageIcon("resources\\" + level + "0" + 
							(generator.nextInt(3) + 1) + "bgm.gif").getImage(), 
					generator.nextInt(300), MOUNTAIN_HEIGHT));
			bgImageMove.add(newMountain);
		}
	}
	
	/**
	 * Returns an Image based on the file name
	 * @param fileName The file name of the image
	 * @return The image of the file name
	 */
	public Image getImage(String fileName)
	{
		return (new ImageIcon(fileName).getImage());
	}
}
